//
//  LogisticsCell.swift
//  letuofu
//
//  Created by WH-JS on 2020/1/8.
//  Copyright © 2020 WH-JS. All rights reserved.
//

import UIKit

class LogisticsCell: QuickTableCellWithInitAndListCount<[String:Any?]> {
    
    static var cellHeightCache : [String:CGFloat] = [:]
    static let infoFont = UIFont.systemFont(ofSize: 12, weight: .medium)
    static let testUILabel = UILabel()
    
    let timeLabel = UILabel()
    let stateLabel = UILabel()
    let pointView = UIView()
    let lineupView = UIView()
    let linedownView = UIView()
    
    override class func heightOf(_ data:[String:Any?])->CGFloat{
        let strKey = data.getString("logisticsInformation") ?? ""
        let strHeight : CGFloat
        if let strHeight_ = LogisticsCell.cellHeightCache[strKey] {
            strHeight = strHeight_
        }else{
            testUILabel.frame = .init(x: 0, y: 0, width: 375 - 30 - 30, height: 999)
            testUILabel.font = LogisticsCell.infoFont
            testUILabel.text = strKey
            testUILabel.numberOfLines = 0
            testUILabel.sizeToFit()
            let strHeight_ = testUILabel.frame.height
            LogisticsCell.cellHeightCache[strKey] = strHeight_
            strHeight = strHeight_
        }
        return 10+12+20+strHeight
    }

    override class func heightAdjust(index: Int, listCount: Int) -> CGFloat {
        if index == listCount-1 {
            return 20
        }
        return 0
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    override func initCell(container: UIView) {
        container.backgroundColor = .white
        drawView(view: pointView) { (make) in
            make.width.height.equalTo(10)
            make.top.equalToSuperview().offset(20+12)
            make.left.equalToSuperview().offset(10)
        }
        pointView.backgroundColor = .x9A9A9A
        pointView.layer.cornerRadius = 5
        
        drawView(view: lineupView) { (make) in
            make.centerX.equalTo(pointView)
            make.width.equalTo(0.5)
            make.top.equalToSuperview()
            make.bottom.equalTo(pointView.snp.centerY)
        }
        lineupView.backgroundColor = .xEEEEEE
        
        drawView(view: linedownView) { (make) in
            make.centerX.equalTo(pointView)
            make.width.equalTo(0.5)
            make.top.equalTo(pointView.snp.centerY)
            make.bottom.equalToSuperview()
        }
        linedownView.backgroundColor = .xEEEEEE
        
        bringSubviewToFront(pointView)
        
        drawLabel(label: timeLabel, text: timeLabel.text, fontSize: 12, fontWeight: .medium, textColor: .x666666) { (make) in
            make.left.equalToSuperview().offset(30)
            make.bottom.equalTo(pointView.snp.top)
        }
        drawLabel(label: stateLabel, text: stateLabel.text, fontSize: 12, fontWeight: .medium, textColor: .x333333) { (make) in
            make.left.equalToSuperview().offset(30)
            make.right.equalToSuperview().offset(-30)
            make.top.equalTo(pointView.snp.bottom)
        }
        stateLabel.numberOfLines = 0
    }
    
    override func fillCell(_ data: [String:Any?], at index: Int, listCount:Int) {
        timeLabel.text = data.getString("dateTime")
        stateLabel.text = data.getString("logisticsInformation")
        if index == 0 {
            pointView.backgroundColor = .xFC4C02
            lineupView.isHidden = true
            linedownView.isHidden = false
        }
        else {
            pointView.backgroundColor = .x999999
            lineupView.isHidden = false
            linedownView.isHidden = (index == listCount-1)
        }
    }
}
